<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>缤格</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/swiper.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/common.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/binge.css"/>
    <script type="text/javascript" src="../../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../../script/swiper.min.js"></script>
    <script type="text/javascript" src="../../script/mobile.js"></script>

    <script type="text/javascript">
        window['adaptive'].desinWidth = 750;
        window['adaptive'].baseFont = 28;
        window['adaptive'].maxWidth = 750;
        window['adaptive'].init();
    </script>
</head>

<body>
<!-- header -->
<div id="header" style="height: 0.9rem; background: #fff; position: fixed; top: 0;left: 0; width: 100%; z-index: 99">
    <div class="header" style="margin-bottom: 0;">
        <div class="head-back">
            <a href="javascript:void(0);" tapmode onclick="backSchool()" class="back-btn"></a>
        </div>
        <div class="head-title">
            <h6>选择你关注的学校</h6>
        </div>
        <!--<div class="head-action">-->
        <!--<a href="javascript:void(0);" tapmode onclick="commonUrl('')" class="action-btn"></a>-->
        <!--</div>-->
    </div>
</div>
<!-- header end -->

<!-- choose school -->
<div class="header-h" style="padding-top: 1.5rem;"></div>
<div class="choose-school">
    <div class="school-kinds">
        <ul id="cityList">
            <!--<li class="on"><a href="javascript:void(0)" tapmode onclick="choose()">北京</a></li>-->
        </ul>
    </div>
    <div class="choose-school-list">
        <h6 class="t-c">关注更多学校(最多可选十所)</h6>
        <ul id="schoolList">
            <!--<li class="on">-->
            <!--<div class="school-img"><img src="../../image/school-icon.png" alt=""></div>-->
            <!--<p>东南大学</p>-->
            <!--</li>-->
            <!--<li class="">-->
            <!--<div class="school-img"><img src="../../image/school-icon.png" alt=""></div>-->
            <!--<p>东南大学</p>-->
            <!--</li>-->
        </ul>
    </div>
</div>

<!-- choose school end -->


</body>
</html>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript">
    var city_id = '';//首位城市id
    var schoolNum = 0;
    apiready = function () {

        setFixStatusBar(); //设置状态栏
        pop.loading();
        keybackListener()

        //获取城市列表
        getMyschool();
        getCity()

    }


    //获取城市列表
    function getCity() {
        var param = {
            token: $api.getStorage('token'),
        }
        getAjax(Interface.get_school_city, param, function (res) {
            var data = res.data;

            var cityList = '';
            var itemNum = 0;
            $.each(data,function (i, item) {

                if (itemNum == 0) {
                    cityList += '<li class="on" tapmode data-id="' + item.id + '"><a href="javascript:void(0)">' + item.city + '</a></li>'
                    city_id = item.id
                } else {
                    cityList += '<li tapmode data-id="' + item.id + '"><a href="javascript:void(0)">' + item.city + '</a></li>'
                }
                itemNum++;

            })

            $("#cityList").html(cityList);
            getSchoollist()

            pop.close();
        }, function (error) {
            pop.notice(error.msg);
            pop.close();
        })
    }

    //获取对应城市的学校列表
    function getSchoollist() {
        var param = {
            token: $api.getStorage('token'),
            city_id: city_id
        }
        getAjax(Interface.school_address, param, function (res) {
            var data = res.data;

            var schoolList = '';
            if (isDefine(data)) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].school_follow == 1 || data[i].school_follow == 3) {
                        schoolList += '<li data-id="' + data[i].id + '" data-status="' + data[i].school_follow + '" class="on"><div class="school-img"><img src="' + ajaxUrl + data[i].icon_path + '" alt=""></div> <p>' + data[i].title + '</p> </li>'
                    } else {
                        schoolList += '<li data-id="' + data[i].id + '" data-status="' + data[i].school_follow + '"><div class="school-img"><img src="' + ajaxUrl + data[i].icon_path + '" alt=""></div> <p>' + data[i].title + '</p> </li>'
                    }

                }
                $("#schoolList").html(schoolList);
            } else {
                $("#schoolList").html(noData);
            }

            pop.close();
        }, function (error) {
            $("#schoolList").html(noData);
            pop.notice(error.msg);
            pop.close();
        })

    }

    function comfirmFollow(stype, school_id, fn,fnError,_this) {
        var type = 2;//1为会员，2为圈子
        //stype： 1为关注，2为取消关注
        follow(stype, type, school_id, function (res) {
            fn(res);
            api.sendEvent({
                name: 'chooseFollowSchool',
                extra: {}
            });
            /*if (stype == 1) {
                pop.notice('关注成功');
                _this.removeClass('on');
            } else {
                pop.notice('已取消关注');
                _this.addClass('on');
            }*/
        },function (error) {
            fnError(error)
        })
    }


    //选择城市
    $(".school-kinds").on('click', 'li', function () {
        var _this = $(this);
        city_id = _this.attr('data-id');
        _this.addClass('on').siblings('li').removeClass('on');
        $('.choose-school-list ul').animate({scrollTop: 0}, 500);//滚动条回到顶端
        getSchoollist()

    });

    function getMyschool() {
        var param = {
            token: $api.getStorage('token'),
        }
        getAjax(Interface.school_list, param, function (res) {

            var list = res.data;
            if (isDefine(list)) {
                schoolNum = list.length;
                console.log(schoolNum)
            }
        }, function (error) {
        })

    }


    //选择学校
    $(".choose-school-list").on('click', 'li', function () {
        var _this = $(this);
        var school_id = _this.attr("data-id");
        var self_school = _this.attr("data-status");

        if (self_school == 3) {
            pop.notice('母校不能取消关注');
            return;
        } else {
            if (_this.hasClass('on')) {
                comfirmFollow(2, school_id, function (res) {
                    pop.notice(res.msg);
                    _this.removeClass('on')

                    api.sendEvent({
                        name: 'chooseFollowSchool',
                        extra: {}
                    });
                    getMyschool()

                },function (error) {
                    // pop.notice(error.msg);
                },_this)
            } else {
                if(schoolNum == 10){
                    console.log(schoolNum)
                    pop.notice('已经关注了10个学校圈');
                    return;
                }
                comfirmFollow(1, school_id, function (res) {
                    pop.notice(res.msg);
                    _this.addClass('on')

                    api.sendEvent({
                        name: 'chooseFollowSchool',
                        extra: {}
                    });
                    getMyschool()

                },function (error) {
                    // pop.notice(error.msg);
                },_this)
            }
        }

    });

    $(".choose-school").height($(window).height() - $(".header").outerHeight(true) - $(".header-h").outerHeight(true))


    function backSchool() {

        api.sendEvent({
            name: 'refreshSchool',
            extra: {}
        });

        comeBack()
    }


</script>
